<?php

class SemesterScoreManager_SemesterInputController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        $this->view->yearSemester = $this->_getParam('yearSemester');
        $this->view->classSn = $this->_getParam('classSn');
        if ($this->view->classSn) {
            $tempArr = explode('-', $this->_getPAram('yearSemester'));
            $year = (int) $tempArr[0];
            $semester = (int) $tempArr[1];

            $this->view->inputKind =   $this->getRequest()->getPost('input_kind');
            $this->view->subjectSn = $this->_getParam('subjectSn');
            $this->view->studentSn = $this->_getParam('studentSn');

            // 預設為科目輸入
            if (!$this->view->inputKind)
            $this->view->inputKind = 1;

            $q = Doctrine_Query::create()
            ->select('a.*')
            ->from('Student_Model_StudentScoreSemester a')
            ->leftJoin('a.Student_Model_Student b')
            ->leftJoin('b.Student_Model_StudentSemester c')
            ->where('c.year=?', $year)
            ->andWhere('c.semester=?', $semester);
            // 學科成績
            if ($this->view->inputKind == 1) {
                $q->andWhere('a.subject_sn=?', $this->view->subjectSn) ;
                $res = $q->fetchArray();
                $arr = array();
                foreach($res as $row)
                $arr[$row['student_sn']] = $row;
            }
            //個人學期成績
            else {
                $q->andWhere('a.student_sn=?', $this->view->studentSn) ;
                $res = $q->fetchArray();
                $arr = array();
                foreach($res as $row)
                $arr[$row['subject_sn']] = $row;
            }
            $this->view->scoreData = $arr;
            $this->view->subjectData = $this->_getSubject();
            $this->view->studentData = $this->_getStudent();
            $this->view->inputKindArr = array(1=>'subject input style', 2=>'student input style');
            //   $this->_getScore();
        }
    }

    /**
     * 儲存全班學科資料
     */
    public function setSubjectAction()
    {
        //教師 sn
        $teacherSn = Teacher_Model_Teacher::getSnById();
        $scores = $this->_getParam('score');
        $descriptions = $this->_getParam('description');
        $subjectSn = (int) $this->_getParam('subjectSn');
        foreach ($scores as $studentSn => $score) {
            $studentSn = (int) $studentSn;

            $studentScoreSemester = new Student_Model_StudentScoreSemester();
            $find = $studentScoreSemester->getTable()->findBySql('student_sn=? AND subject_sn=?',
            array($studentSn, $subjectSn));
            if ($find->count()> 0 ) {
                $studentScoreSemester->assignIdentifier($find->getFirst()->sn);
            }
            $studentScoreSemester->teacher_sn = $teacherSn ;
            if ($score !== '')
            $studentScoreSemester->score = (float)$score;
            else
            $studentScoreSemester->score = NULL;

            $studentScoreSemester->student_sn = $studentSn;
            $studentScoreSemester->subject_sn = $subjectSn;
            if (isset($descriptions[$studentSn]))
            $studentScoreSemester->description = $descriptions[$studentSn];
            $studentScoreSemester->save();

        }

        $this->_helper->json(true);
    }

    public function setStudentAction()
    {
        //教師 sn
        $teacherSn = Teacher_Model_Teacher::getSnById();
        $scores = $this->_getParam('score');
        $descriptions = $this->_getParam('description');
        $studentSn = (int) $this->_getParam('studentSn');
        foreach ($scores as $subjectSn => $score) {
            $subjectSn = $subjectSn;

            $studentScoreSemester = new Student_Model_StudentScoreSemester();
            $find = $studentScoreSemester->getTable()->findBySql('student_sn=? AND subject_sn=?',
            array($studentSn, $subjectSn));
            if ($find->count()> 0 ) {
                $studentScoreSemester->assignIdentifier($find->getFirst()->sn);
            }
            $studentScoreSemester->teacher_sn = $teacherSn ;
            if ($score !== '')
            $studentScoreSemester->score = (float)$score;
            else
            $studentScoreSemester->score = NULL;

            $studentScoreSemester->student_sn = $studentSn;
            $studentScoreSemester->subject_sn = $subjectSn;
            if (isset($descriptions[$subjectSn]))
            $studentScoreSemester->description = $descriptions[$subjectSn];
            $studentScoreSemester->save();

        }

        $this->_helper->json(true);
    }



    /**
     *  取出學生資料
     *
     */
    protected function _getStudent()
    {
        // TODO 轉學生處理


        $tempArr = explode("-", $this->_getParam('yearSemester'));
        $year = (int)$tempArr[0];
        $semester = (int)$tempArr[1];
        $tempArr = explode("-", $this->_getParam('classSn'));
        $grade = (int) $tempArr[0];
        $classSn = (int) $tempArr[1];
        $student = new Student_Model_Student();
        $res = $student->getClassStudent($year,$semester,$grade,$classSn);
        return $res ;
    }


    /**
     * 取得學科資料
     * @return unknown
     */
    protected function _getSubject()
    {
        $tempArr = explode("-", $this->_getParam('yearSemester'));
        $year = (int)$tempArr[0];
        $semester = (int)$tempArr[1];
        $tempArr = explode("-", $this->_getParam('classSn'));
        $grade = (int) $tempArr[0];
        $classSn = (int) $tempArr[1];
        // 學科資料
        $subject = new Semester_Model_SemesterSubject();
        $res = $subject->getSubject($year,$semester,$grade,$classSn);
        //Semester_Model_SemesterSubject::SCORE_MODE_STAGE);
        $arr = array();
        // print_r($res);
        foreach($res as $row) {
            foreach ($row as $val) {
                if ($val['scored'] == 'Y')
                $arr[$val['subject_sn']] = $val;
            }
        }

        return $arr;
    }
}

